from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import *
# Create your views here.

class Register(APIView):
    def post(self,request):
        name = request.data['username']
        usernames = User.objects.filter(username = str(name)).first()
        if usernames:
            return Response({
                'code':200,
                'message':'欢迎',
                'uid':usernames.id
            })
        else:
            return Response({
                'code': 405,
                'message': '错误',
            })

class Shownew(APIView):
    def get(self,request):
        newlist = News.objects.all()
        news = Newsser(newlist,many=True)

        return Response({
            'news':news.data
        })
class Usershow(APIView):
    def get(self,request):
        userlist = User.objects.all()
        userlists = Userser(userlist,many=True)

        return Response({
            'userlists':userlists.data
        })

class Pingluns(APIView):
    def post(self,request):
        pinglun = request.data['pinglun']
        ids = request.data['ids']
        name = request.data['name']
        id = User.objects.get(pk=name)
        ids = News.objects.get(pk=ids)
        P.objects.create(
            Pl = pinglun,
            uid = id,
            nid = ids
        )
        if int(id.user_level) == 1:
            id.score = int(id.score) + 10
            id.save()
        else:
            id.score = int(id.score) + 20
            id.save()
        return Response({
            'message':'评论成功'
        })



